<?php
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'check_user_login.php';
?>
<!DOCTYPE html>
<html>
    <head id="Head1">
        <title>
            Sub Menu - Tesdo Admin
        </title>
        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php';
        ?>
    </head>
    <body>
        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'header.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_pager.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_upload.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_data_input.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_sub_menu.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_menu.php';
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'ckeditor.php';

        //Script Process Page
        //Get objects
        $lib_upload = new lib_upload();
        $dao_sub_menu = new dao_sub_menu();
        $dao_menu = new dao_menu();
        $lib_pager = new lib_pager();
        $lib_data_input = new lib_data_input();

        //-------------Form_Load-------------

        $list_sub_menus = $dao_sub_menu->get_all();

        //CLEAR & CANCEL
        if (!empty($_POST['btnClear']) || !empty($_POST['btnCancel'])) {
            ?>
            <script type="text/javascript">
                window.location = <?php echo LINK_ROOT . DIR_ADMIN . 'sub_menu.php'; ?>;
            </script>
            <?php
        } else {
            //POST process
            if ($_SERVER['REQUEST_METHOD'] == "POST") {
                $action_type = $_POST['a_type'];
                $sub_menu_id = empty($_POST['a_sub_menu_id']) ? 0 : $_POST['a_sub_menu_id'];
                if ($action_type == 'delete' && $sub_menu_id != 0) {
                    if ($dao_sub_menu->delete($sub_menu_id) == 1) {
                        $success = 'Deleted sub_menu with id = ' . $sub_menu_id . ' successfully!';
                    } else {
                        $error = 'Delete sub_menu with id = ' . $sub_menu_id . ' failed!';
                    }
                } else if ($action_type == 'edit' && $sub_menu_id != 0 && empty($_POST['btnSave'])) {
                    //Get Edit data when click on edit icon (not work with btnSave click)
                    $edit_item = $dao_sub_menu->get_by_id($sub_menu_id);
//                    var_dump($edit_item);
                } else if ((empty($_POST['a_title']) ||
                        empty($_POST['a_ddl_menu']) ||
                        empty($_POST['a_content'])
                        ) && !empty($_POST['btnSave'])) {
                    $error = "All the required field must not be empty!";
                } else {
                    //Get post array
                    $post_array = array(
                        'sub_menu_id' => $_POST['a_sub_menu_id'],
                        'sub_menu_title' => $_POST['a_title'],
                        'sub_menu_content' => $_POST['a_content'],
                        
                        'menu_id' => $_POST['a_ddl_menu'],
                    );
                    
                    $post_array['sub_menu_feature_title'] = "";
                    if (!empty($_POST['a_feature_title'])) {
                        $post_array['sub_menu_feature_title'] = $_POST['a_feature_title'];
                    }
                    
                    $post_array['sub_menu_feature_content'] = "";
                    if (!empty($_POST['a_feature_content'])) {
                        $post_array['sub_menu_feature_content'] = $_POST['a_feature_content'];
                    }
                    
                    $post_array['file_upload'] = "";
                    
                    //--------------
                    if ($action_type == 'insert' || $action_type == 'edit') {
                        if ($dao_sub_menu->add($post_array['sub_menu_id'], $post_array['menu_id'], $post_array['sub_menu_title'], $post_array['sub_menu_content'], $post_array['file_upload'], $post_array['sub_menu_feature_title'], $post_array['sub_menu_feature_content'])) {
                            if ($action_type == 'insert') {
                                $success = 'Inserted new Sub Menu';
                            } else {
                                $success = 'Updated information for Sub Menu with id = ' . $post_array['sub_menu_id'];
                            }
                        } else {
                            $error = 'Failed to progess the request!';
                        }
                    }
                }
            }
        }
        //Get list and divide page
        if (!empty($_GET['p'])) {
            $current_page = $_GET['p'];
        } else {
            $current_page = 1;
        }
        $list_total = $dao_sub_menu->get_all();
        $page_size = 10;
        $total_page = $lib_pager->get_total_page($list_total, $page_size);
        $list_current = $lib_pager->get_current_page_list($page_size, $current_page, $list_total);
        ?>

        <div>
            <div class="content-box-outer">
                <div class="content-box-right">
                </div>
                <div class="content-box-left">
                    <h1>Sub Menu</h1>
                    <div class="MessageArea">
                        <?php if (!empty($error)) { ?>
                            <span id="cphAdmin_lblMsg" style="color: red;">
                                <?php echo $error; ?>
                            </span>
                        <?php } ?>
                        <?php if (!empty($success)) { ?>
                            <span id="cphAdmin_lblMsg" style="color: green;">
                                <?php echo $success; ?>
                            </span>
                        <?php } ?>
                    </div>
                    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" id="form_sub_menu" method="POST" enctype="multipart/form-data">
                        <div>
                            <input name="a_sub_menu_id" value="<?php
                            if (empty($edit_item)) {
                                echo '0';
                            } else {
                                echo $edit_item['sub_menu_id'];
                            }
                            ?>" type="hidden">

                            <input name="a_type" value="<?php
                            if (empty($edit_item)) {
                                echo 'insert';
                            } else {
                                echo 'edit';
                            }
                            ?>" type="hidden">

                            
                            <div class="TitleArea"><b>Menu:<span style="color: red;">*</span></b></div>
                            <div class="textBoxArea">
                                <select name="a_ddl_menu" onchange="" id="a_ddl_conference" style="width:300px;">
                                    <?php 
                                    $list_menus = $dao_menu->get_all();
                                    if (empty($list_menus)) { ?>
                                        <script type="text/javascript">
                                            alert("Currently MENU is empty, you must create MENU before you can create Sub Menu!");
                                        </script>
                                        <?php
//                                        lib_redirect::Redirect(DIR_ADMIN . 'conferences.php');
                                    }
                                    foreach ($list_menus as $item) {
                                        ?>
                                        <option value="<?php echo $item['menu_id'] ?>"
                                        <?php
                                        if (!empty($edit_item)) {
                                            if ($edit_item['menu_id'] == $item['menu_id']) {
                                                echo 'selected';
                                            }
                                        }
                                        ?>
                                                ><?php echo $item['menu_title'] ?></option>
                                            <?php } ?>
                                </select>
                            </div>
                            
                            <div class="TitleArea"><b>Title:<span style="color: red;">*</span></b></div>
                            <div class="textBoxArea">
                                <input name="a_title" value="<?php
                                if (empty($edit_item)) {
                                    echo '';
                                } else {
                                    echo $edit_item['title'];
                                }
                                ?>" type="text" id="a_title" style="width:400px;">
                            </div>

                            <div class="TitleArea"><b>Content:<span style="color: red;">*</span></b></div>
                            <div class="textBoxArea">
                                <textarea class="ckeditor" id="a_content" name="a_content">
                                    <?php
                                    if (empty($edit_item)) {
                                        echo '';
                                    } else {
                                        echo $edit_item['content'];
                                    }
                                    ?>
                                </textarea>
                            </div>
                            
                            <div class="TitleArea"><b>Feature Title:</b></div>
                            <div class="textBoxArea">
                                <input name="a_feature_title" value="<?php
                                if (empty($edit_item)) {
                                    echo '';
                                } else {
                                    echo $edit_item['feature_title'];
                                }
                                ?>" type="text" id="a_title" style="width:400px;">
                            </div>
                            
                            <div class="TitleArea"><b>Feature Content:</b></div>
                            <div class="textBoxArea">
                                <textarea class="ckeditor" id="a_content" name="a_feature_content">
                                    <?php
                                    if (empty($edit_item)) {
                                        echo '';
                                    } else {
                                        echo $edit_item['feature_content'];
                                    }
                                    ?>
                                </textarea>
                            </div>
                            
                            <div class="TitleArea"><b>.</b></div>
                            <div class="textBoxArea">
                                <input type="submit" name="btnSave" value="Save" class="buttonDesign">
                                <input type="submit" name="btnClear" value="Clear" class="buttonDesign">
                                <input type="submit" name="btnCancel" value="Cancel" class="buttonDesign">
                            </div>
                        </div>

                        <br clear="all"><br>
                        <h1>Manage Sub Menu</h1>
                        <div>
                            <div>
                                <table cellspacing="0" cellpadding="4" rules="cols" id="cphAdmin_gvJournal" style="color:Black;background-color:White;border-color:#DEDFDE;border-width:1px;border-style:None;width:988px;border-collapse:collapse;">
                                    <tbody><tr style="color:White;background-color:#6B696B;font-weight:bold;">
                                            <th scope="col">ID</th>
                                            <th scope="col">Parent Menu Name</th>
                                            <th scope="col">Title</th>
                                            <th scope="col">Content</th>
                                            <th scope="col">Feature Title</th>
                                            <th scope="col">Feature Content</th>
                                            <th scope="col">Actions</th>
                                        </tr>
                                        <?php
                                        $c = 0;
                                        foreach ($list_current as $i => $item) {
                                            if ($c % 2 == 0) {
                                                ?>
                                                <tr style="background-color:#F7F7DE;"> 
                                                <?php } else { ?>
                                                <tr style="background-color:#FFF;"> 
                                                    <?php
                                                }
                                                ?>
                                                <td>
                                                    <?php echo $item['sub_menu_id']; ?>
                                                </td>
                                                <td>
                                                    <?php echo $item['menu_title']; ?>
                                                </td>
                                                <td>
                                                    <?php echo $item['title']; ?>
                                                </td>
                                                <td>
                                                    <?php echo substr(strip_tags($item['content']), 0, 100); ?>
                                                </td>
                                                <td>
                                                    <?php echo $item['feature_title']; ?>
                                                </td>
                                                <td>
                                                    <?php echo substr(strip_tags($item['feature_content']), 0, 100); ?>
                                                </td>
                                                <td align="center">
                                                    <input type="image" 
                                                           name="btnEdit"
                                                           title="Edit" 
                                                           onclick="set_value('a_type', 'edit');
                                                                       set_value('a_sub_menu_id', <?php echo $item['sub_menu_id']; ?>);
                                                                       submit_form('form_sub_menu');" 
                                                           class="input-edit-image" 
                                                           src="<?php echo LINK_ROOT . DIR_ADMIN_LAYOUT_IMAGE . 'edit.png'; ?>" >
                                                    <input type="image" 
                                                           name="btnDelete" 
                                                           title="Delete" 
                                                           onclick="if (my_confirm()) {
                                                                           set_value('a_type', 'delete');
                                                                           set_value('a_sub_menu_id', <?php echo $item['sub_menu_id']; ?>);
                                                                           submit_form('form_sub_menu');
                                                                       } else {
                                                                           return false;
                                                                       }"
                                                           class="input-delete-image" 
                                                           src="<?php echo LINK_ROOT . DIR_ADMIN_LAYOUT_IMAGE . 'delete.png'; ?>">
                                                </td>
                                            </tr>
                                            <?php
                                            $c++;
                                        }
                                        ?>
                                        <tr align="right" style="color:Black;background-color:#F7F7DE;">
                                            <td colspan="7">
                                                <table>
                                                    <tbody>
                                                        <tr>
                                                            <?php for ($a = 1; $a <= $total_page; $a++) { ?>
                                                                <td>
                                                                    <span>
                                                                        <a href="sub_menu.php?p=<?php echo $a; ?>" <?php if ($current_page != $a) echo 'style="color:Black;"'; ?>><?php echo $a; ?></a>
                                                                    </span>
                                                                </td>
                                                            <?php } ?>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

        </div>

        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'footer.php';
        ?>
    </body>
</html>
